import { ipcMain } from "electron";
import { BrowserWindow } from "electron/main";
import ipcChannel from "./ipcChannel";

const installIpcMain = function (mainWindow: BrowserWindow) {
    installClose(mainWindow)
    installMin(mainWindow);
    installRestore(mainWindow);
};

export default installIpcMain

function installRestore(mainWindow: BrowserWindow) {
    ipcMain.on(ipcChannel.RESTORE_APP, () => {
        if (mainWindow.isMaximized()) {
            mainWindow.unmaximize();
        } else {
            mainWindow.maximize();
        }
    });

    mainWindow.on("resize", () => {
        console.log("[Resize]");
        mainWindow.webContents.send(ipcChannel.RESTORE_APP, mainWindow.isMaximized());
    })
}

function installMin(mainWindow: BrowserWindow) {
    ipcMain.on(ipcChannel.MIN_APP, () => {
        console.log("here min");
        mainWindow.minimize();
    });
}

function installClose(mainWindow: BrowserWindow) {
    ipcMain.on(ipcChannel.CLOSE_APP, () => {
        if (mainWindow) {
            console.log("here close");
            mainWindow.close();
        }
    });
}
